package com.easyedu.helper.service;

import com.easyedu.core.entity.po.Institute;
import com.easyedu.core.entity.po.Student;
import com.easyedu.core.util.RandomUtil;
import com.easyedu.helper.constant.Name;
import com.easyedu.helper.dao.InstituteDao;
import com.easyedu.helper.dao.StudentDao;
import java.util.HashSet;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author mengranliu
 * @date 2022/3/11
 */
@Service
public class StudentService {


     @Autowired
     private InstituteDao instituteDao;

     @Autowired
     private StudentDao studentDao;

     public void createStudent() {

          System.out.println("===== start create student =====");
          //获取所有专业信息

          List<Institute> instituteList = instituteDao.getInstitute();
          int num = 0;
          for (int year = 2018; year < 2022; year++) {
               //便利所有专业
               for (Institute institute : instituteList) {
                    //每个专业生成2～3个班
                    int classRandom = RandomUtil.getRandom(2, 3);
                    for (int i = 1; i <= classRandom; i++) {
                         //每个班生成30～35名学生
                         int stuNumberRandom = RandomUtil.getRandom(30, 35);
                         for (int j = 1; j <= stuNumberRandom; j++) {
                              Student student = new Student();
                              String stuNumberStr =
                                      String.valueOf(year) + String.valueOf(institute.getDepartmentId()) + "0"
                                              + String.valueOf(i) + String.valueOf(j);
                              if (j < 10) {
                                   stuNumberStr =
                                           String.valueOf(year) + String.valueOf(institute.getDepartmentId()) + "0"
                                                   + String.valueOf(i) + "0" + String.valueOf(j);
                              }
                              student.setStudentNumber(stuNumberStr);
                              student.setInstituteId(institute.getInstituteId());
                              student.setInstituteName(institute.getInstituteName());
                              student.setDepartmentId(institute.getDepartmentId());
                              student.setDepartmentName(institute.getDepartmentName());
                              student.setGrade(year);
                              student.setAdministrationClass(institute.getDepartmentId() + "0" + i);
                              String name = getRandomName();
                              student.setName(name);
                              String sex = "女";
                              if (RandomUtil.getRandom(1, 2) == 1) {
                                   sex = "男";
                              }
                              student.setSex(sex);
                              //插入学生
                              studentDao.addStudent(student);
                              num++;
                              System.out.println("=========生成第" + num + "个学生");
                         }
                    }
               }
          }
          System.out.println("===== create student finished =====");
     }

     private static final HashSet<String> nameSet = new HashSet<>();

     private String getRandomName() {
          int i = 0;
          for (; ; ) {
               //生成姓
               String firstName = Name.firstName[RandomUtil.getRandom(0, Name.firstName.length - 1)];
               //生成名
               String lastName = "";
               for (int k = 1; k <= RandomUtil.getRandom(1, 2); k++) {
                    lastName = lastName + Name.lastName[RandomUtil.getRandom(0, Name.lastName.length - 1)];
               }
               String name = firstName + lastName;
               if (!nameSet.contains(name)) {
                    i = 0;
                    nameSet.add(name);
                    return name;
               }
               i++;
               if (i >= 10) {
                    System.out.println("======已经重复" + i + "次========");
               }
          }

     }

}
